package com.vanke.house.api;

import com.vanke.house.api.response.JsonResult;
import com.vanke.house.service.HouseInfoService;
import com.vanke.house.service.SaleHouseService;
import com.vanke.house.service.spider.HouseSpider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author fangyu
 * @version v1.0.0
 * @date 2020/6/18 9:38 下午
 */
@RestController
@RequestMapping("api")
public class ApiController {

    // 构造注入
    private final HouseSpider houseSpider;

    private final HouseInfoService houseInfoService;
    private final SaleHouseService saleHouseService;

    @Autowired
    public ApiController(HouseSpider houseSpider, HouseInfoService houseInfoService, SaleHouseService saleHouseService) {
        this.houseSpider = houseSpider;
        this.houseInfoService = houseInfoService;
        this.saleHouseService = saleHouseService;
    }

    @GetMapping("/house")
    public List getHouseList() {
        return houseInfoService.queryHouseList();
    }

    @PostMapping("/crawl_deal")
    public JsonResult crawlDeal(@RequestParam("url") String url, @RequestParam("type") String type) {
        System.out.println(url);
        try {
            houseSpider.crawl(url, type);
        } catch (IOException e) {
            return JsonResult.failed(e.getMessage());
        }
        return JsonResult.success();
    }

    @GetMapping("/sale")
    public List getSaleHouseList(
            @RequestParam("page") Integer page,
            @RequestParam("pageSize") Integer pageSize
    ) {
        return saleHouseService.querySaleHouseList(page, pageSize);
    }
}
